﻿using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;

namespace VIT.Provider
{
    public class Module : Module_Base
    {
        #region Variable
        string user = HttpContext.Current.Session["userName"].ToString();
        #endregion

        #region Constructor
        public Module(  
                        string LanguageId = "",
                        string TemplateName = "",
                        string TemplatePosition = "",
                        string ComponentName = "",
                        string ComponentPosition = "",
                        string ViewName = "",
                        string Param = "",
                        int Orders = 50,
                        bool Published = true)
        {
            this.LanguageId = LanguageId;
            this.TemplateName = TemplateName;
            this.TemplatePosition = TemplatePosition;
            this.ComponentName = ComponentName;
            this.ComponentPosition = ComponentPosition;
            this.ViewName = ViewName;
            this.Param = Param;
            this.Orders = Orders;
            this.Published = Published;
        }
        #endregion

        #region List
        public DataTable GetAll(string languageid, string orderByField = "TemplatePosition, ComponentPosition, Orders", bool desc = true, bool isTransaction = false)
        {
            string sort = (desc) ? " DESC" : " ";
            string strQuery = "select Id, Name, Title, TemplateName, ComponentName, ViewName, TemplatePosition, ComponentPosition, Param, Orders, Published from Module m join Module_Language la on Id=ModuleId and LanguageID=@LanguageID where Deleted=0 order by " + orderByField + sort + ", m.ModifyDate ";
            SqlParameter LanguageId = new SqlParameter("@LanguageID", languageid);
            return ExecuteDataset(isTransaction, strQuery, LanguageId).Tables[0];
        }

        public bool Delete(int delete, int id, bool isTransaction = false)
        {
            string strQuery = "update Module set Published=@Published, Deleted=@Deleted, ModifyDate=@Now, ModifyByUser=@User, DeleteByUser=@DeleteByUser where Id=@Id";
            SqlParameter Id = new SqlParameter("@Id", id);
            SqlParameter P = new SqlParameter("@Published", (delete == 0) ? 1 : 0);
            SqlParameter D = new SqlParameter("@Deleted", delete);
            SqlParameter User = new SqlParameter("@User", user);
            SqlParameter DeleteByUser = new SqlParameter("@DeleteByUser", (delete==1)?user:"");
            SqlParameter Now = new SqlParameter("@Now", DateTime.Now);

            HttpContext.Current.Application["ModuleData"] = null;
            return ExecuteNonQuery(isTransaction, strQuery, Id, P, D, User, DeleteByUser, Now) > 0;
        }

        public bool Publish(int publish, int id, bool isTransaction = false)
        {
            string strQuery = "update Module set Published=@Published, ModifyDate=@Now, ModifyByUser=@User where Id=@Id";
            SqlParameter Id = new SqlParameter("@Id", id);
            SqlParameter P = new SqlParameter("@Published", publish);
            SqlParameter User = new SqlParameter("@User", user);
            SqlParameter Now = new SqlParameter("@Now", DateTime.Now);

            HttpContext.Current.Application["ModuleData"] = null;
            return ExecuteNonQuery(isTransaction, strQuery, Id, P, User, Now) > 0;
        }

        public bool Order(string order, int id, bool isTransaction = false)
        {
            string strQuery = "update Module set Orders=Orders " + order + " 1, ModifyDate=@Now, ModifyByUser=@User where Id=@Id";
            SqlParameter Id = new SqlParameter("@Id", id);
            SqlParameter User = new SqlParameter("@User", user);
            SqlParameter Now = new SqlParameter("@Now", DateTime.Now);

            HttpContext.Current.Application["ModuleData"] = null;
            return ExecuteNonQuery(isTransaction, strQuery, Id, User, Now) > 0;
        }
        #endregion

        #region Edit
        public void GetSingle(bool isTransaction = false)
        {
            if (Exist(isTransaction))
            {
                string strQuery = "select Name, Title, TemplateName, ComponentName, ViewName, TemplatePosition, ComponentPosition, Param, Orders, Published from Module left join Module_Language on Id=ModuleId and LanguageID=@LanguageId where Id=@Id";
                SqlParameter Id = new SqlParameter("@Id", this.Id);
                SqlParameter LanguageId = new SqlParameter("@LanguageId", this.LanguageId);
                DataTable dt = ExecuteDataset(isTransaction, strQuery, Id, LanguageId).Tables[0];

                if (dt.Rows.Count > 0)
                {
                    this.Name = dt.Rows[0]["Name"].ToString();

                    this.Title = dt.Rows[0]["Title"].ToString();
                    this.TemplateName = dt.Rows[0]["TemplateName"].ToString();
                    this.TemplatePosition = dt.Rows[0]["TemplatePosition"].ToString();
                    this.ComponentName = dt.Rows[0]["ComponentName"].ToString();
                    this.ViewName = dt.Rows[0]["ViewName"].ToString();
                    this.ComponentPosition = dt.Rows[0]["ComponentPosition"].ToString();
                    
                    this.Param = dt.Rows[0]["Param"].ToString();
                    if (!string.IsNullOrEmpty(dt.Rows[0]["Orders"].ToString()))
                        this.Orders = int.Parse(dt.Rows[0]["Orders"].ToString());
                    if (!string.IsNullOrEmpty(dt.Rows[0]["Published"].ToString()))
                        this.Published = (bool)dt.Rows[0]["Published"];
                }
            }
            else this.Id = 0;
        }

        public bool Exist(bool isTransaction = false)
        {
            string strQuery = "select Id from Module where Id=@Id";
            SqlParameter Id = new SqlParameter("@Id", this.Id);
            return ExecuteScalar(isTransaction, strQuery, Id) != null;
        }

        public bool Exist(int id, string languageid, bool isTransaction = false)
        {
            string strQuery = "select ModuleId, LanguageID from Module_Language where ModuleId=@Id and LanguageID=@LanguageId";
            SqlParameter Id = new SqlParameter("@Id", id);
            SqlParameter LanguageId = new SqlParameter("@LanguageId", languageid);
            return ExecuteScalar(isTransaction, strQuery, Id, LanguageId) != null;
        }

        public bool Save(bool isTransaction = true)
        {
            TransactionBegin();
            try
            {
                if (Exist(isTransaction)) // sửa
                {
                    Delete(0, this.Id, isTransaction);
                    UpdateObj(isTransaction);

                    if (Exist(this.Id, this.LanguageId, isTransaction)) UpdateObjLang(isTransaction);
                    else InsertObjLang(isTransaction);
                }
                else // thêm
                {
                    this.Id = InsertObj(isTransaction);
                    InsertObjLang(isTransaction);
                }

                TransactionCommit();
                return true;
            }
            catch
            {
                TransactionRollback();
                return false;
            }
            finally
            {
                HttpContext.Current.Application["ModuleData"] = null;
            }
        }

        public int InsertObj(bool isTransaction = false)
        {
            string sql = "insert into Module(Name, TemplateName, ComponentName, ViewName, TemplatePosition, ComponentPosition, Param, Orders, Published, Deleted, CreateDate, ModifyDate, CreateByUser, ModifyByUser) "
                       + "values (@Name, @TemplateName, @ComponentName, @ViewName, @TemplatePosition, @ComponentPosition, @Param, @Orders, @Published, 0, @Now, @Now, @User, @User)";

            SqlParameter Name = new SqlParameter("@Name", this.Name);
            SqlParameter TemplateName = new SqlParameter("@TemplateName", this.TemplateName);
            SqlParameter ComponentName = new SqlParameter("@ComponentName", this.ComponentName);
            SqlParameter ViewName = new SqlParameter("@ViewName", this.ViewName);
            SqlParameter TemplatePosition = new SqlParameter("@TemplatePosition", this.TemplatePosition);
            SqlParameter ComponentPosition = new SqlParameter("@ComponentPosition", this.ComponentPosition);
            SqlParameter OrderVal = new SqlParameter("@Orders", this.Orders);
            SqlParameter Published = new SqlParameter("@Published", this.Published);
            SqlParameter Param = new SqlParameter("@Param", this.Param);
            SqlParameter User = new SqlParameter("@User", user);
            SqlParameter Now = new SqlParameter("@Now", DateTime.Now);

            return ExecuteNonQuery(isTransaction, sql, Name, TemplateName, ComponentName, ViewName, TemplatePosition, ComponentPosition, Param, OrderVal, Published, User, Now);
        }

        public int InsertObjLang(bool isTransaction = false)
        {
            string sqlLang = "insert into Module_Language (ModuleId, LanguageId, Title, CreateDate, ModifyDate, CreateByUser, ModifyByUser) "
                                            + "values (@ModuleId, @LanguageId, @Title, @Now, @Now, @User, @User)";
            SqlParameter ModuleId = new SqlParameter("@ModuleId", this.Id);
            SqlParameter LanguageId = new SqlParameter("@LanguageId", this.LanguageId);
            SqlParameter Title = new SqlParameter("@Title", this.Title);
            SqlParameter User = new SqlParameter("@User", user);
            SqlParameter Now = new SqlParameter("@Now", DateTime.Now);

            return ExecuteNonQuery(isTransaction, sqlLang, ModuleId, LanguageId, Title, User, Now);
        }

        public int UpdateObj(bool isTransaction = false)
        {
            string sqlCat = "update Module set Name=@Name, TemplateName=@TemplateName, ComponentName=@ComponentName, ViewName=@ViewName, TemplatePosition=@TemplatePosition, ComponentPosition=@ComponentPosition, Param=@Param, Orders=@OrderVal, Published=@Published, ModifyDate=@Now, ModifyByUser=@User where Id=@Id";
            SqlParameter Id = new SqlParameter("@Id", this.Id);
            SqlParameter Name = new SqlParameter("@Name", this.Name);
            SqlParameter TemplateName = new SqlParameter("@TemplateName", this.TemplateName);
            SqlParameter ComponentName = new SqlParameter("@ComponentName", this.ComponentName);
            SqlParameter ViewName = new SqlParameter("@ViewName", this.ViewName);
            SqlParameter TemplatePosition = new SqlParameter("@TemplatePosition", this.TemplatePosition);
            SqlParameter ComponentPosition = new SqlParameter("@ComponentPosition", this.ComponentPosition);
            SqlParameter Param = new SqlParameter("@Param", this.Param);
            SqlParameter OrderVal = new SqlParameter("@OrderVal", this.Orders);
            SqlParameter Published = new SqlParameter("@Published", this.Published);
            SqlParameter User = new SqlParameter("@User", user);
            SqlParameter Now = new SqlParameter("@Now", DateTime.Now);

            return ExecuteNonQuery(isTransaction, sqlCat, Id, Name, TemplateName, ComponentName, ViewName, TemplatePosition, ComponentPosition, Param, OrderVal, Published, User, Now);
        }

        public int UpdateObjLang(bool isTransaction = false)
        {
            string sqlLang = "update Module_Language set Title=@Title, ModifyDate=@Now, ModifyByUser=@User where ModuleId=@ModuleId and LanguageId=@LanguageId";
            SqlParameter ModuleId = new SqlParameter("@ModuleId", this.Id);
            SqlParameter LanguageId = new SqlParameter("@LanguageId", this.LanguageId);
            SqlParameter Title = new SqlParameter("@Title", this.Title);
            SqlParameter User = new SqlParameter("@User", user);
            SqlParameter Now = new SqlParameter("@Now", DateTime.Now);

            return ExecuteNonQuery(isTransaction, sqlLang, ModuleId, LanguageId, Title, User, Now);
        }
        #endregion

        #region Trash
        public DataTable GetRecycleBin(string orderByField = "ModifyDate", bool desc = true, bool isTransaction = false)
        {
            string sort = (desc) ? " DESC" : " ";
            string strQuery = "select Id, Name, Param, TemplateName, ComponentName, ViewName, TemplatePosition, ComponentPosition, CreateDate, ModifyDate, CreateByUser, ModifyByUser, DeleteByUser from Module where Deleted=1 order by " + orderByField + sort;
            return ExecuteDataset(isTransaction, strQuery).Tables[0];
        }

        public bool Cleared(string id, bool isTransaction = false)
        {
            string strQuery = "delete from Module where Id=@Id";
            SqlParameter Id = new SqlParameter("@Id", id);
            return ExecuteNonQuery(isTransaction, strQuery, Id) > 0;
        }
        #endregion
    }
}
